package com.edu.user.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.user.entity.Module;
import com.edu.user.entity.TreeNodeVO;
import com.edu.user.mapper.ModuleMapper;
import com.edu.user.service.IModuleService;
import com.edu.vo.Result;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//extends ServiceImpl<SysUserInfoMapper, SysUserInfo> implements ISysUserInfoService
@Service
public class ModuleServiceImpl extends ServiceImpl<ModuleMapper, Module> implements IModuleService {


    @Override
    public List<Module> queryModuleListHierarchyByAccount(String account) throws Exception {

        return this.baseMapper.queryModuleListHierarchyByAccount(account);

    }

    @Override
    public List<TreeNodeVO> queryTreeNodeListHierarchy() {
        return this.baseMapper.selectTreeNodeListHierarchy();
    }

    @Override
    public List<TreeNodeVO> queryThirdTreeNodeListByRoleId(String roleId) {
        return this.baseMapper.selectThirdTreeNodeListByRoleId(roleId);
    }

    @Override
    public Result grant(String roleId, List<String> moduleIds) {
        this.baseMapper.deleteGrantsByRoleId(roleId);

        for (String moduleId : moduleIds) {
            if (StringUtils.isEmpty(moduleId)) {
                return new Result(false, 1, "参数异常");
            }
            Map<String, Object> map = new HashMap<>();
            map.put("roleId", roleId);
            map.put("moduleId", moduleId);
            int i = this.baseMapper.insertGrant(map);
        }
        return new Result(true, 0, "授权成功");
    }
}
